﻿using AutoMapper;
using ManagementApi.Models.System.Role;
using Modules.Cms;
using Modules.Cms.Entitys;
using Modules.System.Role;
using Modules.System.User;
using PmSoft;

namespace ManagementApi.Models.Cms
{
    public class CmsDealerDto
    {
        /// <summary>
        /// 主键
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public int CreateUserId { get; set; }

        /// <summary>
        /// 创建人名称
        /// </summary>
        public string CreateUserName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDt { get; set; }

        /// <summary>
        /// 更新人ID
        /// </summary>
        public int UpdateUserId { get; set; }

        /// <summary>
        /// 更新人名称
        /// </summary>
        public string UpdateUserName { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateDt { get; set; }

        /// <summary>
        /// 删除标识
        /// </summary>
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 经销商名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 负责人姓名
        /// </summary>
        public string HeadUserName { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 负责人
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        #region 扩展字段
        public string UserName { get; set; }

        /// <summary>
        /// 气瓶总数
        /// </summary>
        public int AllCount { get; set; } = 0;
        /// <summary>
        /// 已租赁数量
        /// </summary>
        public int UsedCount { get; set; } = 0;
        /// <summary>
        /// 可租赁数量
        /// </summary>
        public int HasCount { get; set; } = 0;
        #endregion
    }

    public static partial class CmsDealerExceptions
    {
        public static CmsDealerDto AsDto(this Modules.Cms.Entitys.CmsDealer entity, IEnumerable<CmsOrder> orders,IEnumerable<CmsCustomLease> leases)
        {
            var mapper = ServiceAccessor.GetService<IMapper>();
            var userService = ServiceAccessor.GetService<UserService>();
            CmsDealerDto dto = new CmsDealerDto();
            mapper.Map(entity, dto);
            dto.UserName = userService.Get(entity.UserId)?.Name ?? "";

            #region 获取数量
            var cmsOrders = orders.Where(t => t.DealerId == entity.Id && t.ConsigneeStatus == 20);

            //查询已租气瓶
            var cmsCustomLeaseServiceervice = ServiceAccessor.GetService<CmsCustomLeaseService>();
            var customLeases = cmsCustomLeaseServiceervice.GetCmsCustomLeasesByDealerId(entity.Id).Where(t => t.Status == 10);
            if (cmsOrders.Any())
            {
                int inCount = cmsOrders.Where(t => t.Type == 20).Sum(t => t.OutboundQty) ?? 0;
                int outCount = cmsOrders.Where(t => t.Type == 10).Sum(t => t.OutboundQty) ?? 0;
                dto.AllCount = outCount - inCount;
                dto.UsedCount = customLeases.Count();
                dto.HasCount = dto.AllCount - dto.UsedCount;
            }
            #endregion
            return dto;

        }
    }
}
